package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.UserDao;
import com.hxkj.dao.UserManagementDao;
import com.hxkj.domain.po.SysUser;
import com.hxkj.service.UserManagementService;
import com.hxkj.utils.AESUtil;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.service.sftp.domain.annotation.FormatUrl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserManagementServiceImpl implements UserManagementService {

    @Autowired
    private UserManagementDao userManagementDao;
    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo<SysUser> getList(SysUser sysUser, PageTo pageParam) throws Exception{
        Page<SysUser> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        if(StringUtils.isNotBlank(sysUser.getPhone())){
            sysUser.setPhone(AESUtil.encryptPhoneNumber(sysUser.getPhone()));
        }
        userManagementDao.selectUserList(sysUser);
        PageInfo<SysUser> pageInfo = new PageInfo<>(page);
        for (SysUser user : pageInfo.getList()) {
            if(StringUtils.isNotBlank(user.getPhone())) {
                try{
                    user.setPhone(AESUtil.decryptPhoneNumber(user.getPhone()));
                }catch (Exception e){
                    log.info(e.getMessage());
                    user.setPhone("");
                }
            }
        }
        return pageInfo;
    }

    @Override
    @FormatUrl(type = "after")
    public ResultDataStruct<SysUser> getUser(SysUser sysUser) throws Exception {
        ResultDataStruct<SysUser> result = new ResultDataStruct<>();
        if(sysUser == null){
            result.setCode(701);
            result.setMessage("请填写查询条件");
            return result;
        }
        sysUser = userManagementDao.getUser(sysUser);
        if(sysUser == null){
            result.setCode(702);
            result.setMessage("没有符合条件的用户数据");
            return result;
        }
        result.setCode(200);
        result.setMessage("success");
        result.setData(sysUser);
        return result;
    }

    @Override
    public ResultMessageStruct modifyUserStatus(SysUser sysUser) throws Exception{
        ResultMessageStruct result = new ResultMessageStruct();
        int i = userManagementDao.modifyUserStatus(sysUser);
        if(i == 0){
            result.setCode(701);
            result.setMessage("修改用户使用状态失败!");
            return result;
        }else{
            result.setCode(200);
            String msg ;
            if(sysUser.getDelTag().equals("0")){
                msg = "用户已启用";
            }else{
                msg = "用户已禁用";
            }
            result.setMessage(msg);
            return result;
        }
    }

    @Override
    public ResultDataStruct<List<SysUser>> selectAllUserId() throws Exception {
        ResultDataStruct<List<SysUser>> result = new ResultDataStruct<>();
        List<SysUser> list = userManagementDao.selectAllUserId();
        result.setData(list);
        return result;
    }

    @Override
    public ResultDataStruct<SysUser> selectUserByAccount(SysUser sysUser) throws Exception {
        ResultDataStruct<SysUser> result = new ResultDataStruct<>();
        sysUser = userManagementDao.selectUserByAccount(sysUser);
        if(sysUser == null){
            result.setCode(705);
            result.setMessage("没有符合条件的数据");
            return result;
        }
        result.setCode(200);
        result.setMessage("success");
        result.setData(sysUser);
        return result;
    }

    @Override
    public ResultMessageStruct modifyUser(SysUser sysUser) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        if(StringUtils.isNotBlank(sysUser.getPhone())){
            sysUser.setPhone(AESUtil.encryptPhoneNumber(sysUser.getPhone()));
        }
        int i = userManagementDao.modifyUser(sysUser);
        if(i == 0){
            result.setCode(701);
            result.setMessage("修改用户失败!");
            return result;
        }else{
            result.setCode(200);
            result.setMessage("success");
            return result;
        }
    }
}
